import { Injectable } from '@angular/core';
import { Lyric, Song } from '@services/data-types/common.type';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { Observable, forkJoin } from 'rxjs';
import { first } from 'rxjs/operators';
import { SongService } from '@services/song.service';

type SongData = [Song, Lyric];

@Injectable()
export class SongInfoResolveService implements Resolve<SongData> {
  constructor(private songService: SongService) {}
  resolve(route: ActivatedRouteSnapshot): Observable<SongData> {
    const id = Number(route.paramMap.get('id'));
    return forkJoin(
      this.songService.getSongDetail(route.paramMap.get('id')),
      this.songService.getLyric(id)
    ).pipe(first());
  }
}
